//
//  ST_introEditView.swift
//  btvideo
//
//  Created by mac on 2019/11/11.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit


class ST_introEditView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(titleLabel)
        self.addSubview(textView)
        self.addSubview(placeholderLabel)
        self.addSubview(countLabel)
        
        self.borderColor = Color.init(hexString: "#F3F3F3")
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(24)
        }
        
        textView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalTo(countLabel.snp.top).offset(-7)
        }
        
        placeholderLabel.snp.makeConstraints { (make) in
            make.left.equalTo(textView).offset(5)
            make.top.equalTo(textView).offset(7)
            make.right.equalTo(textView).offset(-5)
        }
        
        
        countLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-12)
            make.right.equalToSuperview().offset(-15)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        superview?.layoutSubviews()
        
        
        self.borderWidth = 2
        self.cornerRadius = 12
    }
    
//    MARK: - Properties
    
    lazy var titleLabel:UILabel = {
        let control = UILabel()
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.textColor = UIColor.init(hex: 0x333333)
        return control
    }()
    
    
    lazy var textView:UITextView = {
       let control = UITextView()
        control.textColor = Color.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.delegate = self
        control.tintColor = Color.init(hex: 0x333333)
        
//        control.backgroundColor = Color.init(hex: 0x555555, transparency: 0.4)
        
        return control
    }()
    
    lazy var placeholderLabel:UILabel = {
       let control = UILabel()
        control.textColor = Color.init(hex: 0x999999)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.numberOfLines = 0
        return control
    }()
    
    lazy var countLabel:UILabel = {
        let control = UILabel()
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
        control.textColor = UIColor.init(hex: 0x999999)
        control.textAlignment = .right
        
//        control.backgroundColor = Color.random
        return control
    }()
    
//    MARK: - Bind Model
    var model:ST_IntroEditViewModel?{
        didSet{
            
            guard let m = self.model else { return  }
            
            placeholderLabel.text = m.placeholder
            placeholderLabel.isHidden = !m.text.isEmpty
            
            textView.text = m.text
            titleLabel.text = m.title
            let length = m.text.count
            countLabel.text = "\(length)/150"
            
        }
    }
    
}


extension ST_introEditView:UITextViewDelegate{
    
    func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !textView.text.isEmpty
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.borderColor = Color.init(hexString: "#FEC207")
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.borderColor = Color.init(hexString: "#F3F3F3")
    }
    
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        let mutableStr = NSMutableString.init(string: textView.text)
        
        mutableStr.replaceCharacters(in: range, with: text)
        
        countLabel.text = String.init(format: "%d/150", mutableStr.length)
        
        countLabel.textColor = mutableStr.length > 150 ? UIColor.init(hexString: "#FF1F56") : UIColor.init(hexString: "#999999")
        
        if mutableStr.length > 160{
            return false
        }
        
        return true
        
    }
    
    
}

//MARK: - Model
struct ST_IntroEditViewModel {
    var title:String
    var text:String
    var placeholder:String
}
